/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.boundary;

import it.polimi.meteocal.control.SearchManager;
import it.polimi.meteocal.control.UserManager;
import it.polimi.meteocal.entity.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name = "searchBean")
@SessionScoped

public class SearchBean implements Serializable {

    @EJB
    private UserManager um;

    @EJB
    private SearchManager sm;

    private String searchedUser;
    private User selectedUser;
    private List<User> users;

    public SearchBean() {
        System.out.println("************INSTANTIATE**************");
    }

    public String getUser() {
        return searchedUser;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }

    public void setUser(String user) {
        System.out.println("settato user " + user);
        this.searchedUser = user;

    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void search() {
        System.out.println("**************SEARCHBEAN************** " + searchedUser);

        List<User> result = sm.search(searchedUser);

        if (result != null && !result.isEmpty()) {
            for (int i = 0; i < result.size(); i++) {
                System.out.println(result.get(i));
                if (um.getLoggedUser().getEmail().equals(result.get(i).getEmail())) {
                    result.remove(i);
                }
            }
        } else {
            System.out.println("Risultto nullo");
        }
        users = new ArrayList<User>(result);
    }

    public String goToCalendar() {
        System.out.println("**************GOTOCALENDARBEAN************** " + selectedUser.getEmail());
        searchedUser = "";
        users.clear();
        return ("homeOther?faces-redirect=true&user=" + selectedUser.getEmail());
    }

    public String turnBack() {
        System.out.println("************TURNBACK**************");
        searchedUser = "";
        selectedUser = null;
        if (users != null) {
            if (!users.isEmpty()) {
                users.clear();
            }
        }
        return "homepage";
    }

    public boolean ispossibleAction() {
        boolean b = false;

        if (selectedUser != null) {
            if (selectedUser.getPublicCalendar() == false) {
                b = true;
            }
        }

        return (b);
    }

    public String getStyle(User u) {
        if (u.getPublicCalendar() == true) {
            return ("highPublic");
        }
        return ("highPrivate");
    }

    public String getImage() {
        if (selectedUser != null) {
            if (!selectedUser.getPublicCalendar()) {
                return ("privatecalendar.png");
            } else {
                return ("publiccalendar.png");

            }
        }
        return ("");
    }

    public String goBack() {
        searchedUser = "";
        users.clear();
        return "homepage";
    }

}
